//
//  MeiShiDetailViewController.m
//  MyProject
//
//  Created by apple on 15/11/9.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "MeiShiDetailViewController.h"
#import "AFNetworking.h"
#import "MeiShiDetailModel.h"
#import "MeiShiDetailListModel.h"
#import "JSONModelArray.h"
#import "UIImageView+AFNetworking.h"
#import "LZXHelper.h"
#import "CanTingViewController.h"
#import "CanTingDetailViewController.h"
#define URL @"http://www.youyuwei.com/api/rest?&list=listofCity+&cityid=10057"
@interface MeiShiDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *_dataArr;
    JSONModelArray *_dishlistArr;
    JSONModelArray *listArr;
}

@property (nonatomic,strong)UITableView *tableView;
@end

@implementation MeiShiDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataArr = [[NSMutableArray alloc]init];
    
    self.title = self.titleName;
    
    [self getData];
    
    [self createView];
}

-(void)getData
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:URL parameters:@{@"cityid":self.ID} success:^(AFHTTPRequestOperation *operation, id responseObject) {
       
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil][@"data"];
        NSArray * dishlist = dic[@"dishlist"];
        _dishlistArr = [[JSONModelArray alloc]initWithArray:dishlist modelClass:[MeiShiDetailModel class]];
        listArr = [[JSONModelArray alloc]initWithArray:dic[@"list"] modelClass:[MeiShiDetailListModel class]];
        
        [_dataArr addObject:_dishlistArr];
        [_dataArr addObject:listArr];
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

-(void)createView
{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:0];
    [self.view addSubview:self.tableView];
    _tableView.delegate =self;
    _tableView.dataSource = self;
    
    [_tableView registerNib:[UINib nibWithNibName:@"MeiShiDetailCell" bundle:nil] forCellReuseIdentifier:@"567"];
}

#pragma mark -代理方法 －  
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArr.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[_dataArr objectAtIndex:section] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr = [_dataArr objectAtIndex:indexPath.section];
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"567"];
    //图片
    UIImageView *imgV = (UIImageView *)[cell.contentView viewWithTag:1000];
    //圆角
    imgV.layer.cornerRadius = 5;
    imgV.layer.masksToBounds= YES;
    
    //名字
    UILabel *lable = (UILabel *)[cell.contentView viewWithTag:1001];
    //介绍
    UILabel *lable1 = (UILabel *)[cell.contentView viewWithTag:1002];
    lable.backgroundColor = [UIColor colorWithHue:15/255.f saturation:15/255.f brightness:15/255.f alpha:0.6];
    if (indexPath.section == 0) {
        MeiShiDetailModel *model = [arr objectAtIndex:indexPath.row];
        [imgV setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:nil];
        
        lable.text = model.name;
        
        lable1.text = model.sum;
        
    }else{
        MeiShiDetailListModel *model = [arr objectAtIndex:indexPath.row];
        
        [imgV setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:nil];
       // NSLog(@"%@",model.cover);
        lable.text = model.name;
        lable1.text = model.summary;
    
    }
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    float height = 0;
    if (indexPath.section == 0) {
        MeiShiDetailModel *model = [[_dataArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        height = [LZXHelper textHeightFromTextString:model.sum width:[UIScreen mainScreen].bounds.size.width - 20 fontSize:15];
        
    }else{
        MeiShiDetailListModel *model = [[_dataArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        height = [LZXHelper textHeightFromTextString:model.summary width:[UIScreen mainScreen].bounds.size.width - 20 fontSize:15];
       
    }
    return 220+height;
    
}

//点击了某个cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr = [_dataArr objectAtIndex:indexPath.section];
    if (indexPath.section == 0) {
        MeiShiDetailModel *model = [arr objectAtIndex:indexPath.row];
        CanTingViewController *canting = [[CanTingViewController alloc]init];
        canting.ID = model.id;
        [self.navigationController pushViewController:canting animated:YES];
    }else{
        MeiShiDetailListModel *model = [arr objectAtIndex:indexPath.row];
        CanTingDetailViewController *canting = [[CanTingDetailViewController alloc]init];
        canting.ID = model.id;
        [self.navigationController pushViewController:canting animated:YES];
    }
}






-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40)];
    lable.textAlignment = 1;
    if (section == 0) {
        lable.text = @"当地美食推荐";
    }else{
        lable.text = @"著名餐厅";
    }
    
    
    return lable;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return 0.001;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
